package newcoder.t2;

import java.util.ArrayList;

//coder am i
//i ma redoc
public class Solution {
    public static void main(String[] args) {
//        String s = ReverseSentence("nowcoder. a am I");
//        System.out.println(s);


        int[] a = {2, 3, 4, 2, 6, 2, 5, 1};
        ArrayList<Integer> result = maxInWindows(a, 3);
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }

    public static String ReverseSentence(String str) {
        char[] chars = str.toCharArray();
        int length = chars.length;
        char[] c = new char[length];
        for (int i = 0; i < length; i++) {
            c[length - i - 1] = chars[i];
        }
        int left = 0, right = 0;
        while (left < length) {
            if (c[left] == ' ') {
                left++;
            }
            right = left;
            while (right < length && c[right] != ' ') {
                right++;
            }
            int start = left, end = right - 1;
            while (start < end) {
                char temp = c[start];
                c[start] = c[end];
                c[end] = temp;
                start++;
                end--;
            }
            left = right;
            while (left < length && c[left] == ' ') {
                left++;
            }
        }
        return new String(c);
    }

    public static ArrayList<Integer> maxInWindows(int[] num, int size) {
        int length = num.length;
        if (size == 0 || size > length) {
            return new ArrayList<>();
        }
        int left = 0, right = left + size - 1;
        ArrayList<Integer> list = new ArrayList<>();
        int maxIndex = -1;
        while (right < length) {
            if (maxIndex >= left) {
                //直接比较新数和原最大数
                if (num[maxIndex] <= num[right]) {
                    maxIndex = right;
                }
                list.add(num[maxIndex]);
            } else {
                int temp = 0;
                for (int i = left; i <= right; i++) {
                    if (num[i] >= temp) {
                        temp = num[i];
                        maxIndex = i;
                    }
                }
                list.add(temp);
            }
            left++;
            right++;
        }
        return list;
    }
}
